package io.github.jsp.tranvolume.controller;

import io.github.jsp.tranvolume.dto.Result;
import io.github.jsp.tranvolume.service.TranVolumeMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 交易量监控控制器
 */
@RestController
@RequestMapping("/api/tranvolume")
@CrossOrigin(origins = "*")
public class TranVolumeController {

    @Autowired
    private TranVolumeMonitorService monitorService;

    /**
     * 获取省份交易量环比数据
     */
    @GetMapping("/province/compare")
    public Result<Map<String, Object>> getProvinceCompareData(
            @RequestParam(required = false) String provCode,
            @RequestParam(required = false) String servicePlat,
            @RequestParam(required = false) String chelCode,
            @RequestParam(required = false, defaultValue = "7") Integer timeRange) {
        try {
            Map<String, Object> data = monitorService.getProvinceCompareData(provCode, servicePlat, chelCode, timeRange);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取省份交易量环比数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取中心交易量实时监控数据
     */
    @GetMapping("/center/realtime")
    public Result<Map<String, Object>> getCenterRealtimeData(
            @RequestParam(required = false) String center,
            @RequestParam(required = false) String provCode,
            @RequestParam(required = false) String servicePlat,
            @RequestParam(required = false) String chelCode,
            @RequestParam(required = false, defaultValue = "6") Integer timeRange) {
        try {
            Map<String, Object> data = monitorService.getCenterRealtimeData(center, provCode, servicePlat, chelCode, timeRange);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取中心交易量实时数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取失败响应码趋势数据
     */
    @GetMapping("/fail-code/trend")
    public Result<Map<String, Object>> getFailCodeTrendData(
            @RequestParam(required = false) String center,
            @RequestParam(required = false) String provCode,
            @RequestParam(required = false) String servicePlat,
            @RequestParam(required = false) String chelCode,
            @RequestParam(required = false, defaultValue = "6") Integer timeRange,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        try {
            Map<String, Object> data = monitorService.getFailCodeTrendData(center, provCode, servicePlat, chelCode, timeRange, limit);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取失败响应码趋势数据失败：" + e.getMessage());
        }
    }

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public Result<String> healthCheck() {
        return Result.success("交易量监控服务运行正常");
    }
}